<?php

//商品栏目
if(!defined('IN_T')){
    die('hacking attempt');
}

//查看栏目列表
if($act == "index"){

    $res = productCatList();
    $tp->assign('list',$res);
}
//编辑商品列表
else if($act == "detail"){

    $company_type_id = intval($_REQUEST['company_type_id']);
    //验证$company_type_id是否合法
    $company_type = $Db->query("SELECT * FROM ".$Base->table('company_type')." WHERE id = $company_type_id","Row");
    if(empty($company_type)){
        $company_type_id = 0;
    }
    //前台显示
    if(empty($_POST)){
        //展示所有分类
        $classification = $Db->query("SELECT * FROM ".$Base->table('company_type')."");
        $tp->assign('classification',$classification);
        $tp->assign('company_type',$company_type);
    }
    //后台数据处理
    else{
        $res['status'] = 0;
        $data = array(
            'name'=>Common::sfilter($_POST['name']),
            'brief'=>Common::sfilter($_POST['brief']),
        );

        if(empty($data['name'])){
            $res['msg'] = '类型名不能为空';
        }
        // else if(empty($data['sort'])){
        // 	$res['msg'] = '排序不能为空';
        // }
        else{
            if($company_type_id){
                $Db->update($Base->table('company_type'),$data,array('id'=>$company_type_id));
            }else{
                $company_type_id = $Db->insert($Base->table('company_type'),$data);
            }
            //判断一级类 sort值为id的100
            // if(empty($data['pid'])){
            // 	$Db->update($Base->table('company_company_type'),array('sort'=>$company_type_id.'00'),array('id'=>$company_type_id));
            // }
            $res = array('status'=>1,'msg'=>'提交成功','href'=>'/'.COMPANY_PATH.'/?m=company_type');
        }
        echo $Json->encode($res);
        exit;
    }

}

//删除栏目分类
else if($act =="delete"){
    $id = intval($_REQUEST['company_type_id']);
    // var_dump($id);
    $res['status'] = 0;
    //检测该分类下是否有商户
    $companys = $Db->query("SELECT id FROM ".$Base->table('company_profile')." WHERE type=$id");
    // print_r($companys);
    if(empty($companys)){
        $Db->execSql("delete from ".$Base->table('company_type')." where id=$id ");
        $res['status'] = 1;
    }else{
        $res['msg'] = '该类型有商家存在，无法删除！';
    }
    echo $Json->encode($res);
    exit;
}

$tp->assign('nav','商家类型');
$tp->assign('act',$act);

function productCatList(){
    $sql = "SELECT * FROM ".$GLOBALS['Base']->table('company_type')." ORDER BY id";
    $list = $GLOBALS['Db']->query($sql);
    // var_dump($list);
    return array('list'=>$list);
}

?>